package aut;

import dao.DAOFactory;
import dao.IAirportDAO;
import dao.tro.Airport;
import dao.tro.Country;

import java.util.List;

public class AutoList {

    public StringBuffer getList(String parameter, String targetId) throws WrongAttributeException {
        switch (parameter) {
            case "country":
                return getCountryList(targetId);
            case "airport":
                return getAirportList(targetId);
            default:
                throw new WrongAttributeException();
        }
    }

    private StringBuffer getCountryList(String targetId) {
        StringBuffer stringBuffer = new StringBuffer();
        List<Country> allCountryList = DAOFactory.getDAOFactory(1).getCountryDAO().allCountryList();
        for (Country country: allCountryList) {
            if (country.getName().trim().toLowerCase().startsWith(targetId)) {
                stringBuffer.append("<country>");
                stringBuffer.append("<name>");
                stringBuffer.append(country.getName());
                stringBuffer.append("</name>");
                stringBuffer.append("</country>");
            }
        }
        return stringBuffer;
    }

    private StringBuffer getAirportList(String targetId) {
        StringBuffer stringBuffer = new StringBuffer();
        IAirportDAO factory = DAOFactory.getDAOFactory(1).getAirportDAO();
        List<Airport> allAirportList = factory.allAirportList();
        for (Airport airport: allAirportList) {
            if(airport.getName().trim().toLowerCase().startsWith(targetId)) {
                stringBuffer.append("<airport>");
                stringBuffer.append("<name>");
                stringBuffer.append(airport.getName());
                stringBuffer.append("</name>");
                stringBuffer.append("<incountry>");
                stringBuffer.append(factory.loadCountryName(airport).getInCountry().getName());
                stringBuffer.append("</incountry>");
                stringBuffer.append("</airport>");
            }
        }
        return stringBuffer;
    }
}
